package org.eCon.maven.mojo;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.plugin.MojoExecutionException;
import org.eCon.maven.generatorFactory.GWTCompilePluginGeneratorImpl;
import org.eCon.maven.iPlugin.ICompile;

import java.io.*;
/**
 *
 * User: eCon
 * Date: 04-Aug-2008
 * Time: 22:12:46
 *
 */

/**
 * This plugin is written for GWTCompiler.
 * It should work in(windows, linux and mac).
 * available parameters:
 * gwt_os
 * gwt_relative_out(required)
 * gwt_relative_src
 * gwt_logLevel
 * gwt_gen
 * gwt_tree(true/false)
 * gwt_style
 * gwt_module
 * <p/>
 * If a directory doesn't exist, this plugin should create it.
 *
 * @goal compile
 * @phase compile
 */
public class GWTCompileMojo extends AbstractGWTMojo {

    /**
     * gwt Compiler's logLevel
     *
     * @parameter expression="${gwtCompile.gwt_logLevel}"
     */
    private String gwt_logLevel;
    /**
     * gwt Compiler's generated Files Directory
     *
     * @parameter expression="${gwtCompile.gwt_gen}"
     */
    private String gwt_gen;
    /**
     * gwt Compiler's treeLogger
     *
     * @parameter expression="${gwtCompile.gwt_tree}" default-value="false"
     */
    private boolean gwt_tree;
    /**
     * gwt Compiler's style
     *
     * @parameter expression="${gwtCompile.gwt_style}"
     */
    private String gwt_style;

    /**
     * If GWT compilation or test compilation fails and this parameter is true, Maven will not continue
     * processing the phase
     *
     * @parameter default-value="true"
     */
    protected boolean stopIfCompilationFails;


    public void execute()
            throws MojoExecutionException {

        getLog().info("Start processing eCon's gwtCompiler plugin");
        this.checkPathes();

        ICompile builder = new GWTCompilePluginGeneratorImpl().createPlugin(this.gwt_os);
        getLog().info("Operating Sys: " + builder);
        getLog().info("GWT_HOME: " + this.gwt_Home);
        getLog().info("JAVA_HOME: " + this.java_Home);

        builder.setCurrentDir(this.currentDirAsFile.getAbsolutePath());
        builder.setLog(getLog());
        builder.setJava_Home(this.java_Home);
        builder.setGwt_Home(this.gwt_Home);

        builder.setGwt_gen(this.gwt_gen);
        builder.setGwt_logLevel(this.gwt_logLevel);
        builder.setGwt_module(this.gwt_module);
        builder.setGwt_relative_out(this.gwt_relative_out);
        builder.setGwt_relative_src(this.gwt_relative_src);
        builder.setGwt_style(this.gwt_style);
        builder.setGwt_tree(this.gwt_tree);
        builder.setAdditional_classpathes(additional_classpathes);
        builder.setJvm_param(this.jvm_param);


        String cmd = builder.createGWTCompileString();
        this.executeCommand(cmd);
    }


    protected void executeCommand(String cmd) throws MojoExecutionException {
        getLog().info("Command to be executed:" + cmd);

        BufferedReader input = null;
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            String line;
            input = new BufferedReader(new InputStreamReader(process.getInputStream()));

            while ((line = input.readLine()) != null) {
                getLog().info(line);
            }
            int result = process.waitFor();
            getLog().info("************** CMD Result:" + result + "**********************");
            getLog().info("*** Command executed ***");

            if (result != 0 && stopIfCompilationFails) {
                throw new MojoExecutionException("Failed Command:" + cmd);
            }
        }
        catch (IOException e) {
            getLog().error(e.getMessage());
            throw new MojoExecutionException(this.stack2string(e));
        } catch (InterruptedException e) {
            getLog().error(e.getMessage());
            throw new MojoExecutionException(this.stack2string(e));
        }catch (Exception e){
            getLog().error(e.getMessage());
            throw new MojoExecutionException(this.stack2string(e));
        }
        finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException io) {
                    getLog().error(io.getMessage());
                }
            }
        }
    }


}
